package gov.kqqzyyy.monkey.orderpad.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Objects;

import gov.kqqzyyy.monkey.orderpad.R;
import gov.kqqzyyy.monkey.orderpad.mytools.DateTimeUtil;
import gov.kqqzyyy.monkey.orderpad.object.SelectedDish;
import gov.kqqzyyy.monkey.orderpad.object.post_param.StoredOrder;

/**
 * Created by Monkey on 2018/3/6.
 * 保存订单信息适配器
 */

public class OrderStoredAdapter extends BaseAdapter {

    private final LayoutInflater inflater;
    private List<StoredOrder> list;

    public OrderStoredAdapter(Context context) {
        inflater = LayoutInflater.from(context);
    }

    public void setList(List<StoredOrder> list) {
        Collections.sort(list, (arg0, arg1) -> arg0.getDdh().compareTo(arg1.getDdh()));
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (list != null) {
            return list.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (list != null && list.get(position) != null) {
            return list.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.item_order_stored, parent, false);
            holder.ddh_tv = convertView.findViewById(R.id.item_ddh_text);
            holder.brxx_tv = convertView.findViewById(R.id.item_brxx_text);
            holder.pssj_tv = convertView.findViewById(R.id.item_pssj_text);
            holder.cpxx_tv = convertView.findViewById(R.id.item_cpxx_text);
            holder.zjg_tv = convertView.findViewById(R.id.item_zjg_text);
            holder.dcsj_tv = convertView.findViewById(R.id.item_dcsj_text);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        StoredOrder storedOrder = list.get(position);
        if (storedOrder != null) {
            String ddh = storedOrder.getDdh() == null ? "" : storedOrder.getDdh();
            holder.ddh_tv.setText(ddh);
            String brxxText = "住院号:" + storedOrder.getZyh() + "\n" + "床位号:" + storedOrder.getCwh() + "\n" + "姓名:" + storedOrder.getBrxm();
            holder.brxx_tv.setText(brxxText);
            String scsjText = storedOrder.getScsj() == null ? "" : storedOrder.getScsj();
            holder.pssj_tv.setText(scsjText);
            Calendar calendar = Calendar.getInstance();
            Date xdsj = DateTimeUtil.getDFormatDate(storedOrder.getXdsj());
            calendar.setTime(Objects.requireNonNull(xdsj));
            calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DATE) + 1);
            String scsj = DateTimeUtil.getDFFormatString(calendar.getTime());
            scsj += "\n" + storedOrder.getScsj();
            int cc = Character.getNumericValue(ddh.charAt(ddh.length() - 2));
            switch (cc) {
                case 1:
                    scsj += "\n" + "早餐";
                    break;
                case 2:
                    scsj += "\n" + "午餐";
                    break;
                case 3:
                    scsj += "\n" + "晚餐";
                    break;
            }
            holder.pssj_tv.setText(scsj);
            Gson gson = new Gson();
            List<SelectedDish> dishList = gson.fromJson(storedOrder.getSdcp(), new TypeToken<List<SelectedDish>>() {
            }.getType());
            StringBuilder cpsl = new StringBuilder();
            if (dishList != null) {
                for (SelectedDish dishInfo : dishList
                        ) {
                    if (cpsl.toString().equals("")) {
                        cpsl.append(dishInfo.getCPMC()).append("*").append(dishInfo.getCPSL());
                    } else {
                        cpsl.append("\n").append(dishInfo.getCPMC()).append("*").append(dishInfo.getCPSL());
                    }
                }
            }
            holder.cpxx_tv.setText(cpsl.toString());
            holder.zjg_tv.setText(storedOrder.getZjg());
            holder.dcsj_tv.setText(storedOrder.getXdsj());
        }
        return convertView;
    }

    private static class ViewHolder {
        TextView ddh_tv;
        TextView brxx_tv;
        TextView pssj_tv;
        TextView cpxx_tv;
        TextView zjg_tv;
        TextView dcsj_tv;
    }
}
